<?php
class User{
	//Internal properties
	private $_id;
	private $_name;
	private $_groups;
	private $_permissions;
	
	function __construct( ){
		//Uses global configuration
		global $_CONF;
		//Global action router
		global $_actionRouter;
		//logger
		$_logger = new Logger( );
		
		//Default user informations
		$this->_permissions = array( );
		$this->_groups = array( );
		$this->_name = 'Anonymous';
		$this->_id = '0';
		
		//Retrieves user id  from session
		if( isset( $_SESSION['uid'] ) ){
			
			//Database connection
			$_db = new MysqlDB( );	
			
			$_USER_ID = $_SESSION['uid'];
			
			//Retrieves user data from database
			if( $_db->connectionReady( ) ){
			
				//Retrieves user name
				$_mysql_query_get_username_by_id =
				"
					SELECT username FROM users AND status = {$_CONF['userStatus']['active']}
						WHERE id = {$_USER_ID}
				";
				$_rs = $_db->monitoredQuery( $_mysql_query_get_username_by_id );
				//If resultset is not empty
				if( $_rs and $_rs->num_rows == 1 ){
					
					//Retrieve user's user groups
					$_mysql_query_get_user_user_groups = 
					"
						SELECT user_group_id
							FROM sys_user_user_groups
								WHERE user_id = {$_USER_ID}
					";
					
					$_rs = $_db->monitoredQuery( $_mysql_query_get_user_user_groups );
					
					//Query error
					if( !$_rs ){
						//Log
						$_logger->log( $_CONF['log_levels']['FATAL_ERROR'] , "Query error while retrievering user associated user groups", 0, '' );
						//Redirect 500
						header( 'HTTP/1.1 500 Internal Server Error' );
						header( 'Location: ' . $_actionRouter->getLanguedPath( 'server_error' ) );
						exit;
					}
					//User groups extraction
					$_user_groups = array( );
					while( $row = mysqli_fetch_assoc( $_rs ) ){
						$_user_groups[] = $row['user_group_id'];
					}
					$this->_groups = $user_groups ;
					$user_groups = implode( $_user_groups, ',' );
					
					//If user belogns to any usergroup
					if( count( $_user_groups ) ){				
						//Retrieve permissions ids
						$_mysql_query_select_user_groups_permissions = 
						"
							SELECT permission_id
								FROM sys_user_permissions_user_groups
									WHERE user_group_id IN ( {$user_groups} )
						";
						$_rs =  $_db->monitoredQuery( $_mysql_query_select_user_groups_permissions );
						
						//Query error
						if( !$_rs ){
							//Log
							$_logger->log( $_CONF['log_levels']['FATAL_ERROR'] , "Query error while retrievering groups associated permissions", 0, '' );
							//Redirect 500
							header( 'HTTP/1.1 500 Internal Server Error' );
							header( 'Location: ' . $_actionRouter->getLanguedPath( 'server_error' ) );
							exit;
						}
						
						//Permissions extraction
						$_user_permissions = array( );
						while( $row = mysqli_fetch_assoc( $_rs ) ){
							$_user_permissions[] = $row['permission_id'];
						}
						
						$this->_permissions = $_user_permissions ;
					
					}
				
				}else{
					//Log
					$_logger->log( $_CONF['log_levels']['FATAL_ERROR'] , "User not found: User id - {$_USER_ID} ", 0, '' );
					//Redirect 500
					header( 'HTTP/1.1 500 Internal Server Error' );
					header( 'Location: ' . $_actionRouter->getLanguedPath( 'server_error' ) );
					exit;
				}
			
			}
		
		}
		
	}
	
	//Getters
	public function getUserId( ){
		return $this->_id;
	}
	public function getUserName( ){
		return $this->_name;
	}
	public function getUserGroups( ){
		return $this->_groups;
	}
	public function getUserPermissions( ){
		return $this->_permissions;
	}
	
	//Permissions checking
	//hasPermissions( $permissions )
	//Returns if the current user satify a certain permission set requirement
	public function hasPermissions( $permissions ){
		$user_permissions = $this->getUserPermissions( );
		foreach( $permissions as $permission ){
			if( !in_array( $user_permissions, $permission ) ){
				return false;
			}
		}
		return true;
	}
		
}
?>